package com.example.myapplication.customizeView.loginpage;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import com.example.myapplication.R;

public class LoginKeyboard extends LinearLayout implements View.OnClickListener {
    private static final String TAG = "LoginKeyboard";
    private OnKeyPressListener mKeyPressListener = null;

    public LoginKeyboard(Context context) {
        this(context,null);
    }

    public LoginKeyboard(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoginKeyboard(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.num_key_pad,this);
        initView();
    }

    private void initView() {
        this.findViewById(R.id.number_1).setOnClickListener(this);
        this.findViewById(R.id.number_2).setOnClickListener(this);
        this.findViewById(R.id.number_3).setOnClickListener(this);
        this.findViewById(R.id.number_4).setOnClickListener(this);
        this.findViewById(R.id.number_5).setOnClickListener(this);
        this.findViewById(R.id.number_6).setOnClickListener(this);
        this.findViewById(R.id.number_7).setOnClickListener(this);
        this.findViewById(R.id.number_8).setOnClickListener(this);
        this.findViewById(R.id.number_9).setOnClickListener(this);
        this.findViewById(R.id.number_0).setOnClickListener(this);
        this.findViewById(R.id.back).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int valueId = v.getId();

        if (mKeyPressListener==null){
            return;
        }
        if (valueId == R.id.back){
            //走back
            mKeyPressListener.OnBackPress();
        }else{
            //走数字结果

              String text = ((TextView) v).getText().toString();
                Log.d(TAG,"click value is --->>>"+text);

            mKeyPressListener.OnNumberPress(Integer.parseInt(text));

        }


    }
    public void setOnKeyPressListener(OnKeyPressListener listener){
        this.mKeyPressListener = listener;
    }
    public interface OnKeyPressListener{
        void OnNumberPress(int number);
        void OnBackPress();
    }
}
